/*
 * 1.5
 * Write a method to replace all spaces in a string with ‘%20’.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *replace_space(const char *str)
{
    int len = strlen(str);
    int len2 = len;
    for (int i = 0; i < len; i++) {
        if (str[i] == ' ') {
            len2 += 3;
        }
    }
    char *s = (char *)malloc(len2+1);
    int j = 0;
    for (int i = 0; i < len; i++) {
        if (str[i] != ' ') {
            s[j++] = str[i];
        } else {
            s[j++] = '%';
            s[j++] = '2';
            s[j++] = '0';
        }
    }
    return s;
}

int main()
{
    char *s = replace_space("111 22 333  ");
    printf("%s\n", s);
    free(s);
    return 0;
}
